package yf_service

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"peilian-api/app/global/variable"
	"peilian-api/utils/tools"

	"github.com/gin-gonic/gin"
)

const (
	GenerateVideoCreateType = "youshi_video_create"
	GenerateVideoFinishType = "youshi_video_finish"
)

type YfApi struct{}

// 获取接口凭证 access_token
func (y *YfApi) SendMsg(ctx *gin.Context, topic, typ, message string) (string, error) {
	data := map[string]interface{}{
		"topic":    topic,   // topic
		"type":     typ,     // type类型
		"msg_info": message, // 消息内容
	}
	reqJsonData, _ := json.Marshal(data)
	fmt.Println(string(reqJsonData))
	resp, err := http.Post(
		variable.Config.Yf.Url,
		"application/json",
		bytes.NewReader(reqJsonData),
	)
	if err != nil {
		return "", err
	}
	if resp != nil {
		defer resp.Body.Close()
	}

	res := make(map[string]interface{})
	err = tools.ReadResponseBodyCtx(ctx, resp, &res)
	if err != nil {
		return "", err
	}
	if code, _ := res["code"].(float64); code != 200 {
		return "", fmt.Errorf("接口请求错误")
	}
	b, _ := json.Marshal(res["data"])
	return string(b), nil
}
